/**
 * 
 */
package com.qhgrain.api.comm.datadictionary.provider;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.dubbo.common.json.JSONArray;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.qhgrain.api.comm.DataDictionaryInfo;
import com.qhgrain.api.comm.IDataDicDubboProvider;
import com.qhgrain.api.comm.datadictionary.entity.DataDictionary;
import com.qhgrain.api.comm.datadictionary.service.IDataDictionaryService;

/**
 * @author Administrator
 *
 */       
@Service(value="dataDicDubboProvider")
public class DataDicDubboProvider implements IDataDicDubboProvider {

	@Autowired
	private IDataDictionaryService dataDictionaryService;

	

	private DataDictionary cloneFormVo(DataDictionaryInfo dataDictionaryInfo) {
		DataDictionary dataDictionary = new DataDictionary();
		try {
			PropertyUtils.copyProperties(dataDictionary, dataDictionaryInfo);
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return dataDictionary;

	}

	private DataDictionaryInfo cloneToVo(DataDictionary dataDictionary) {
		DataDictionaryInfo dataDictionaryInfo = new DataDictionaryInfo();
		try {
			PropertyUtils.copyProperties(dataDictionaryInfo, dataDictionary);
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return dataDictionaryInfo;

	}
	
	
	@Override
	public void insertDataDic(DataDictionaryInfo dataDictionaryInfo) {
		DataDictionary dataDictionary = cloneFormVo(dataDictionaryInfo);
		dataDictionaryService.insertDic(dataDictionary);
	}

	@Override
	public DataDictionaryInfo getDataDicInfoByCode(String dcCode) {
		DataDictionary dataDictionary = dataDictionaryService.getDataDicInfoByCode(dcCode);
		DataDictionaryInfo dataDictionaryInfo = cloneToVo(dataDictionary);
		return dataDictionaryInfo;
	}


	@Override
	public String getPagedDataDic(Integer pageNo, Integer pageSize,
			Map<String, String> conditionMap) {
		Page<DataDictionary> page = dataDictionaryService.queryListForPage(pageNo, pageSize, conditionMap);
		PageInfo<DataDictionary> pageInfo = page.toPageInfo();
		String rs = JSONObject.fromObject(pageInfo).toString();
		return rs;
	}

	


	@Override
	public void deleteDataDicById(String dicId) {
		dataDictionaryService.deleteById(dicId);
		
	}

	@Override
	public DataDictionaryInfo getDataDicInfoById(String dicId) {
		DataDictionary dataDictionary =  dataDictionaryService.getDataDicById(dicId);
		DataDictionaryInfo dataDictionaryInfo = cloneToVo(dataDictionary);
		return dataDictionaryInfo;
	}



	@Override
	public void updateDataDic(DataDictionaryInfo dataDictionaryInfo) {
		DataDictionary dataDictionary = cloneFormVo(dataDictionaryInfo);
		dataDictionaryService.update(dataDictionary);
	}

	@Override
	public String getDataDicTree(String sysCode, String dicPcode) {
		List<DataDictionary> dataDictionaries = dataDictionaryService.getChildDataDic(sysCode,dicPcode);
		String rs = net.sf.json.JSONArray.fromObject(dataDictionaries).toString();
		return rs;
	}

	@Override
	public String getChildDataDic(String sysCode, String dcPcode) {
		List<DataDictionary> dataDictionaries = dataDictionaryService.getChildDataDic(sysCode,dcPcode);
		PageInfo<DataDictionary> pageInfo = new PageInfo<DataDictionary>(dataDictionaries);
		return net.sf.json.JSONObject.fromObject(pageInfo).toString();
	}

	@Override
	public String getSystemList() {
		List<Map<String, String>> lists = dataDictionaryService.getSystemList();
		String rs = net.sf.json.JSONArray.fromObject(lists).toString();
		return rs;
	}


}
